<?php
/**
 * ADICIONA SISTEMA
 */
if(	isset($_POST['module']) && $_POST['module'] == 'system' && 
	isset($_POST['action']) && $_POST['action'] == 'add'){
	$name 			= $_POST['name'];
	$description 	= $_POST['description'];
	$system = new System();
	$system->set_name($name);
	$system->set_description($description);
	$system->new_System() or die("erro ao criar sistema");
}

class System{

	private $idSystem;
	private $name;
	private $description;
	private $dtCreation;
	
	function getSystem($id){
		$q 					= "SELECT * FROM system WHERE idSystem='$id'";
		$query 				= mysql_query("$q") or die(mysql_error());
		$query 				= mysql_fetch_array($query, MYSQL_BOTH);
		$this->idUser		= $query['idSystem'];
		$this->name			= $query['name'];
		$this->description	= $query['description'];
		$this->dtCreation	= $query['dtCreation'];
		return true;
	}
	
	/*
	 *  Funções GET
	 */

	function get_idSystem(){		return $this->idSystem;}	//Retorna o Id do Systema
	function get_name(){		return $this->name;}	//etorna o Nome do Systema
	function get_description(){	return $this->description;}	//etorna o Nome do Usuário
	function get_dtCreation(){	return $this->dtCreation;}
	
	function set_idSystem($id){
		$this->idSystem = $id;
		return true;
	}//Insere o Id do Usuário
	function set_name($name){
		$this->name = $name;
		return true;
	}//Insere o Nome do Usuário
	function set_description($description){
		$this->description = $description;
		return true;
	}//Insere a Senha do Usuário
	function set_dtCreation($dtCreation){
		$this->dtCreation = $dtCreation;
		return true;
	}//Insere A Data de Registro do Usuário
	
	function save_System(){
		$q = 	"UPDATE `rpgcom`.`system` 
				SET		`name` 			= '$this->name',
						`description`	= '$this->description', 
						`dtCreation`	= '$this->dtCreation'
				WHERE 	user.idSystem 	= '$this->idSystem'";
		$query = mysql_query("$q") or die(mysql_error());
		if($query)return true;else return false;
	}//Salva Alterações no Perfil
	
	function new_System(){
		$q = "INSERT INTO system(name,
		                       description,
		                       dtCreation)
		                VALUES( '$this->name',
		                		'$this->description',
		                		CURDATE());";
		$query = mysql_query("$q") or die(mysql_error());
		$q = "SELECT idSystem FROM system WHERE name = '$this->name';";
		$query = mysql_query("$q") or die(mysql_error());
		$query = mysql_fetch_array($query, MYSQL_BOTH);
		if($query)return $query['idSystem'];else return false;
	}//Registra novo usuario
	
	function buildSelectSystem(){
		$query = "SELECT system.name, system.idSystem FROM system";
		$result = mysql_query($query) or die(mysql_error());
		$numRows = mysql_num_rows($result);
		$cont = 0;
		$arr_table_result=mysql_fetch_full_result_array($result);
		if($numRows > 0){
			echo("<select name='system'>");
			while($cont<$numRows){
				$system 		= 	$arr_table_result[$cont]['name'];
				$id 			= 	$arr_table_result[$cont]['idSystem'];
				echo("<option value='$id'>$system</option>");
			$cont++;
			}
			echo"</select>";
		}else{echo("n&atilde;o existem systemas cadastrados.");}
	}
	
}
?>